﻿function AirTypeAnalyseBar(container, config) {

    this.series = [];
    this.seriesParam = config["seriesParam"] || [];
    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;
    this.stationName = config["stationName"] || "";
    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];
    this.xCate = config["xCate"] || [];


    this.container = container;
}

AirTypeAnalyseBar.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table class="table_tooltip">';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td  style="width:40px;"><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td style="width:30px;">' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;
};


AirTypeAnalyseBar.prototype.getStep = function () {
    var _ = this,
        width = $("#" + _.container).width(),
        count = _.TimePoint.length,
        num = 1,
        xWidth = _.xwidth;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

AirTypeAnalyseBar.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            type: 'column',
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        lang: {
            contextButtonTitle: '导出图片',
            loading: '加载中',
            noData: '没有数据'
        },
        title: {
            text: '空气质量等级偏差比例图'
        },

        xAxis: [{
            categories: _.xCate
        }],
        yAxis: {
            min: 0,
            title: {
                text: '百分比(%)'
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        tooltip: {
            shared: true,
            //crosshairs: true,
            crosshairs: [
             {
               width: 1,
             }
            ],
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        series: _.series
    }
};

AirTypeAnalyseBar.prototype.onChartLoad = function () {
    this.removeLogo();
};

AirTypeAnalyseBar.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

AirTypeAnalyseBar.prototype.initData = function (jsonData) {
    var _ = this;

    //转换为百分比
    for (var i = 0; i < jsonData[0].length; i++) {
        var tempdata0 = jsonData[0][i];
        var tempdata1 = jsonData[1][i];
        var tempdata2 = jsonData[2][i];
        var tempdata3 = jsonData[3][i];
        var tempdata4 = jsonData[4][i];
        var tempdata5 = jsonData[5][i];
        var tempdata_1 = jsonData[-1][i];
        var tempdata_2 = jsonData[-2][i];
        var tempdata_3 = jsonData[-3][i];
        var tempdata_4 = jsonData[-4][i];
        var tempdata_5 = jsonData[-5][i];
        var total = tempdata0 + tempdata1 + tempdata2 + tempdata3 + tempdata4 + tempdata5 + tempdata_1 + tempdata_2 + tempdata_3 + tempdata_4 + tempdata_5;
        jsonData[0][i] = Math.round((tempdata0 / total) * 100, 0);
        jsonData[1][i] = Math.round((tempdata1 / total) * 100, 0);
        jsonData[2][i] = Math.round((tempdata2 / total) * 100, 0);
        jsonData[3][i] = Math.round((tempdata3 / total) * 100, 0);
        jsonData[4][i] = Math.round((tempdata4 / total) * 100, 0);
        jsonData[5][i] = Math.round((tempdata5 / total) * 100, 0);
        jsonData[-1][i] = Math.round((tempdata_1 / total) * 100, 0);
        jsonData[-2][i] = Math.round((tempdata_2 / total) * 100, 0);
        jsonData[-3][i] = Math.round((tempdata_3 / total) * 100, 0);
        jsonData[-4][i] = Math.round((tempdata_4 / total) * 100, 0);
        jsonData[-5][i] = Math.round((tempdata_5 / total) * 100, 0);


    }

    _.series.push({
        name: "5",
        color: "#4474a5",
        data: jsonData[5]
    });
    _.series.push({
        name: "4",
        color: "#5290ce",
        data: jsonData[4]
    });
    _.series.push({
        name: "3",
        color: "#73a9df",
        data: jsonData[3]
    });
    _.series.push({
        name: "2",
        color: "#a1c7ed",
        data: jsonData[2]
    });
    _.series.push({
        name: "1",
        color: "#d4e7f9",
        data: jsonData[1]
    });
    _.series.push({
        name: "0",
        color: "#52b53e",
        data: jsonData[0]
    });
    _.series.push({
        name: "-1",
        color: "#c0edfe",
        data: jsonData[-1]
    });

    _.series.push({
        name: "-2",
        color: "#a8def2",
        data: jsonData[-2]
    });

    _.series.push({
        name: "-3",
        color: "#93d3eb",
        data: jsonData[-3]
    });

    _.series.push({
        name: "-4",
        color: "#76c1dd",
        data: jsonData[-4]
    });
    _.series.push({
        name: "-5",
        color: "#56adcd",
        data: jsonData[-5]
    });
    _.createChart();
};

AirTypeAnalyseBar.prototype.removeLogo = function () {
    var chartText = $("text[text-anchor='end']");
    chartText.each(function () {
        if ($(this).text() == 'Highcharts.com') {
            $(this).remove();
        }
    });
}



